//负责人：监督官

// 登录对话框
package com.java.view;
import com.java.datamanage.StudentManageSystem;
import com.java.model.Admin;
<<<<<<< HEAD
import picture.Background;
import picture.ButtonUtils;
import picture.WindowUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class LoginDialog extends JDialog {
    //用户名输入框
    private JTextField usernameField;
    //密码输入框
    private JPasswordField passwordField;
    //标记是否验证通过
    private boolean authenticated = false;
    //管理员对象
    private Admin admin;
    //系统核心业务类
=======
import javax.swing.*;
import java.awt.*;


public class LoginDialog extends JDialog {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private boolean authenticated = false;
    private Admin admin;
>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07
    private StudentManageSystem system;

    public LoginDialog(JFrame parent, StudentManageSystem system) {
        super(parent, "管理员登录", true);
<<<<<<< HEAD
        this.system = system;
=======
>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07
        initUI();
    }

    private Admin initUI() {
<<<<<<< HEAD
        setSize(400, 500);
        //居中显示
        setLocationRelativeTo(null);
        setResizable(true);
        //垂直排列
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        // 上下留 80px 空白，左右留白
        contentPanel.setBorder(BorderFactory.createEmptyBorder(80, 50, 80, 50));

        // 3. 初始化组件：标签、文本框、按钮
        JLabel usernameLabel = new JLabel("用户名:");
        // 扩大文本框：宽度20列，或用 setPreferredSize 精确控制
        usernameField = new JTextField(20);

        JLabel passwordLabel = new JLabel("密码:");
        // 扩大密码框：宽度20列
        passwordField = new JPasswordField(20);

        //设置自定义按钮图标：登录和注册
        JButton loginButton = ButtonUtils.createIconButton("picture/按钮素材/屏幕截图 2025-07-18 163852.png", 80, 30);
        JButton registerButton = ButtonUtils.createIconButton("picture/按钮素材/屏幕截图 2025-07-18 163920.png", 80, 30);
        //如果图标加载失败，生物质回退文本
        if(loginButton.getIcon() == null) {
            loginButton.setText("登录");
            loginButton.setPreferredSize(new Dimension(100, 40));
        }
        if(registerButton.getIcon() == null) {
            registerButton.setText("注册");
            registerButton.setPreferredSize(new Dimension(100, 40));
        }


        //设置logo和背景图
        WindowUtils.setLogo(this);
        Background background = new Background("picture/其他素材/Snipaste_2025-07-18_19-31-54.png");
        background.setLayout(new BorderLayout());
        background.add(contentPanel, BorderLayout.CENTER);
        setContentPane(background);


        // 顶部空白
        contentPanel.add(Box.createVerticalGlue());
        contentPanel.add(usernameLabel);
        // 标签和文本框间距
        contentPanel.add(Box.createVerticalStrut(10));
        contentPanel.add(usernameField);
        // 文本框和密码标签间距
        contentPanel.add(Box.createVerticalStrut(20));
        contentPanel.add(passwordLabel);
        // 标签和密码框间距
        contentPanel.add(Box.createVerticalStrut(10));
        contentPanel.add(passwordField);
        // 密码框和登录按钮间距
        contentPanel.add(Box.createVerticalStrut(30));
        contentPanel.add(loginButton);
        // 登录和注册按钮间距
        contentPanel.add(Box.createVerticalStrut(20));
        contentPanel.add(registerButton);
        // 底部空白（平衡顶部，让组件垂直居中）
        contentPanel.add(Box.createVerticalGlue());




        // 事件处理
        // 检验用户名、密码是否正确

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText().trim();
                String password = new String(passwordField.getPassword()).trim();
                //输入法合格性校验
                //1.用户名为空
                if (username.isEmpty()) {
                    JOptionPane.showMessageDialog(LoginDialog.this,
                            "请输入用户名", "提示", JOptionPane.WARNING_MESSAGE);
                    usernameField.requestFocus();
                }
                //2.密码为空
                if (password.isEmpty()) {
                    JOptionPane.showMessageDialog(LoginDialog.this,
                            "请输入密码", "提示", JOptionPane.WARNING_MESSAGE);
                    passwordField.requestFocus();
                }
                //3.不为空时，对管理员的用户名和密码进行校验
                authenticated = system.authenticate(username, password);
                //信息校验成功
                if (authenticated) {
                    admin = system.getAdmin();
                    JOptionPane.showMessageDialog(LoginDialog.this, "登录成功！", "提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    dispose(); // 关闭对话框
                } else {
                    //登录失败
                    JOptionPane.showMessageDialog(LoginDialog.this, "用户名或密码错误", "失败",
                            JOptionPane.ERROR_MESSAGE);
                }


            }
        });
        //注册按钮事件
=======
        setSize(350, 200);
        setLayout(new GridLayout(4, 2, 10, 10));
        setLocationRelativeTo(null);
        setResizable(false);

        // UI组件初始化
        JLabel usernameLabel = new JLabel("用户名:");
        usernameField = new JTextField();

        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField();

        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");

        // 添加组件
        add(usernameLabel);
        add(usernameField);
        add(passwordLabel);
        add(passwordField);
        add(new JLabel()); // 占位
        add(loginButton);
        add(new JLabel()); // 占位
        add(registerButton);

        // 事件处理
        loginButton.addActionListener(e -> {
            // 这里可以添加登录验证逻辑
            authenticated = true;
            dispose(); // 关闭对话框
        });

>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07
        registerButton.addActionListener(e -> {
            RegisterFrame registrationDialog = new RegisterFrame(LoginDialog.this, system);
            registrationDialog.setVisible(true);
        });
<<<<<<< HEAD
=======

>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07
        return admin;
    }

    // 验证
    public boolean isAuthenticated() {
        return authenticated;
    }

    // 获得管理员对象
    public Admin getAdmin() {
        return admin;
    }
<<<<<<< HEAD
}
=======
    }

>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07


